import { itemsDataSelect } from "@/api";
import store from "@/store";
import dayjs from "dayjs";
import { ref } from "vue";

// 根据当前时间判断所属餐别的mealType并返回
export const getCurrentMealType = () => {
  try {
    const currentDate = dayjs().format("YYYY-MM-DD"); // 获取当前时间并格式化为YYYY-MM-DD

    for (const rule of mealRules.value) {
      const startTime = dayjs(`${currentDate} ${rule.startEatTime}`); // 解析开始时间
      const endTime = dayjs(`${currentDate} ${rule.endEatTime}`); // 解析结束时间
      const current = dayjs(); // 解析当前时间

      // 如果当前时间在餐别的时间范围内
      if (
        (current.isAfter(startTime) && current.isBefore(endTime)) ||
        current.isSame(startTime) ||
        current.isSame(endTime)
      ) {
        console.log("当前餐别", rule.name);
        return rule.mealType; // 返回餐别的mealType
      }
    }
    return ""; // 如果没有找到匹配的餐别，返回
  } catch (error) {
    console.error("计算当前餐别失败", error);
    return ""; //
  }
};

// 餐别规则
const mealRules = ref([
  // {
  //   startEatTime: "00:27:29",
  //   endEatTime: "14:27:29",
  //   name: "早餐",
  //    mealType: 1,
  // },
  // {
  //   startEatTime: "14:30:20",
  //   endEatTime: "23:21:20",
  //   name: "午餐",
  //    mealType: 2,
  // },
]);

// 初始化餐别规则
export const initMealRules = async () => {
  try {
    const pam = { dataType: 4, useStatus: 1, businessId: store.getters.businessInfo.businessId };
    const { data } = await itemsDataSelect(pam);
    mealRules.value = data;
  } catch (error) {
    console.error("查询餐别规则失败", error);
  }
};
